<template>
  <div class="bgw">
    <div class="socialTitle clearfix">
      <div
        class="titleName fl"
        v-html="title"
      ></div>
      <!-- <div class="fr" v-show="!changePwd" @click="resetPwd"><el-button type="text">修改密码</el-button></div> -->
    </div>
    <div style="width:40%;padding:30px 0;margin:0 auto">
      <!-- <el-form label-width="80px" :model="accountInfo" v-show="!changePwd">
        <el-form-item label="姓名">
          <el-input v-model="accountInfo.name" readonly></el-input>
        </el-form-item>
        <el-form-item label="工号">
          <el-input v-model="accountInfo.worknumber" readonly></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="accountInfo.phone" readonly></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="accountInfo.apartment" readonly></el-input>
        </el-form-item>
        <el-form-item label="岗位">
          <el-input v-model="accountInfo.position" readonly></el-input>
        </el-form-item>
      </el-form> -->
      <!-- 修改密码 -->
      <el-form
        :model="password"
        status-icon
        :rules="rules2"
        ref="password"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="原始密码"
          prop="oldpassword"
        >
          <el-input
            type="password"
            v-model="password.oldpassword"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="新密码"
          prop="password"
        >
          <el-input
            type="password"
            v-model="password.newpassword"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="确认密码"
          prop="twice"
        >
          <el-input
            type="password"
            v-model="password.twice"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('password')"
          >提交</el-button>
          <el-button @click="cancel('password')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "",
  components: {},
  data () {
    var checkOld = (rule, value, callback) => {
      if (!value.trim()) {
        return callback(new Error("原始密码不能为空"));
      } else {
        callback();
      }
    };
    var validateOnce = (rule, value, callback) => {
      if (value.trim() === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.newpassword.twice.trim() !== "") {
          this.$refs.newpassword.validateField("twice");
        }
        callback();
      }
    };
    var validateTwice = (rule, value, callback) => {
      if (value.trim() === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.password.newpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      title: "密码修改",
      changePwd: false,
	  userinfo:{},
      accountInfo: {
        name: "",
        worknumber: "",
        phone: "",
        apartment: "",
        position: ""
      },
      password: {
        oldpassword: "",
        newpassword: "",
        twice: ""
      },
      rules2: {
        oldpassword: [{ validator: checkOld, trigger: "blur" }],
        newpassword: [{ validator: validateOnce, trigger: "blur" }],
        twice: [{ validator: validateTwice, trigger: "blur" }]
      }
    };
  },
  mounted () { 
	this.userinfo = JSON.parse(localStorage.getItem('userinfo'));
	// console.log(localStorage.getItem('userinfo'));
	console.log(this.userinfo);
  },
  methods: {
    resetPwd () {
      this.changePwd = true;
      this.title = "修改密码";
    },
    submitForm (formName) {
		console.log(this.password);
		if(this.userinfo.grade === '10'){
			this.$refs[formName].validate(valid => {
				if (valid) {
					this.$axios.post("/system/v1/platform/user/passwordUpdate", this.password).then(res => {
						console.log(res);
						if (res.data.message === 'success.') {
							let params = {
								account: this.userinfo.account,
								oldpassword: this.password.oldpassword,
								newpassword: this.password.newpassword
							}
							console.log(params);
							this.$axios.$post('https://zjsy.fcw-china.com/housekeeping/Login/updatePassword',params).then(res => {
								console.log(res);
								if(res.data.code === 1000){
									this.$telAxios.post('/v1/librarian/updatePassWord',{
										accountNumber: this.userinfo.account,
										oldPass: this.password.oldpassword,
										passWord: this.password.newpassword
									}).then(res => {
										console.log(res);
										if(res.data.code === 1000){
											this.$confirm("密码修改成功,请重新登陆", "提示", {
												showClose: false,
												distinguishCancelAndClose: false,
												showCancelButton: false,
												closeOnClickModal: false,
												closeOnPressEscape: false,
												confirmButtonText: "确定",
												cancelButtonText: "取消",
												type: "warning"
											}).then(() => {
												localStorage.clear()
												this.$router.push(
												  { path: "/login" }
												)
											})
										}else{
											this.$message({
												message: res.data.message,
												type: 'warning'
											});
										}
									})
								}else {
									this.$message({
										message: res.data.message,
										type: 'warning'
									});
								}
							})
						} else {
							this.$message({
								message: res.data.message,
								type: 'warning'
							});
						}
					})
				  // this.cancel("password");
				}
			});
		}else if(this.userinfo.grade === '40'){
			let params = {
				personnelId: localStorage.getItem('userid'),
				oldPassword: this.password.oldpassword,
				newPassord: this.password.newpassword
			}
			this.$ceshiGetAxiosys("/v1/common/upadtepersonPassword", params).then(res => {
				 if(res.data.code==1){
				this.$confirm("密码修改成功,请重新登陆", "提示", {
					showClose: false,
					distinguishCancelAndClose: false,
					showCancelButton: false,
					closeOnClickModal: false,
					closeOnPressEscape: false,
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(() => {
					localStorage.clear()
					this.$router.push(
					  { path: "/login" }
					)
				})
				}else {
					this.$message.warning(res.data.message)				 
				}
				
			})
			
			
		}
		else if(this.userinfo.type === 'housekeep'){
			let params = {
				account: this.userinfo.account,
				oldpassword: this.password.oldpassword,
				newpassword: this.password.newpassword
			}
			console.log(params);
			this.$axios.$post('https://zjsy.fcw-china.com/housekeeping/Login/updatePassword',params).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					this.$confirm("密码修改成功,请重新登陆", "提示", {
						showClose: false,
						distinguishCancelAndClose: false,
						showCancelButton: false,
						closeOnClickModal: false,
						closeOnPressEscape: false,
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					}).then(() => {
						localStorage.clear()
						this.$router.push(
						  { path: "/login" }
						)
					})
				}else{
					this.$message({
					message: res.data.message,
					type: 'warning'
					});
				}
			})
		}else if(this.userinfo.type === 'backstage'){
			let params = {
				account: this.userinfo.account,
				oldpassword: this.password.oldpassword,
				newpassword: this.password.newpassword
			}
			this.$axios.post("/system/v1/platform/user/passwordUpdate", params).then(res => {
				console.log(res);
				if (res.data.message === 'success.') {
					this.$confirm("密码修改成功,请重新登陆", "提示", {
						showClose: false,
						distinguishCancelAndClose: false,
						showCancelButton: false,
						closeOnClickModal: false,
						closeOnPressEscape: false,
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					}).then(() => {
						localStorage.clear()
						this.$router.push(
						  { path: "/login" }
						)
					})
				}else{
					this.$message({
						message: res.data.message,
						type: 'warning'
					});
				}
			})
		}else if(this.userinfo.grade == '30'){
			this.$refs[formName].validate(valid => {
				if (valid) {
					this.$axios.post("/system/v1/platform/user/passwordUpdate", this.password).then(res => {
						if (res.data.message === 'success.') {
							this.$telAxios.post('/v1/librarian/updatePassWord',{
								accountNumber: this.userinfo.account,
								oldPass: this.password.oldpassword,
								passWord: this.password.newpassword
							}).then(res => {
								console.log(res);
								if(res.data.code === 1000){
									this.$confirm("密码修改成功,请重新登陆", "提示", {
										showClose: false,
										distinguishCancelAndClose: false,
										showCancelButton: false,
										closeOnClickModal: false,
										closeOnPressEscape: false,
										confirmButtonText: "确定",
										cancelButtonText: "取消",
										type: "warning"
									}).then(() => {
										localStorage.clear()
										this.$router.push(
										  { path: "/login" }
										)
									})
								}else{
									this.$message({
										message: res.data.message,
										type: 'warning'
									});
								}
							})
						} else {
							this.$message({
								message: res.data.message,
								type: 'warning'
							});
						}
					})
				}
			});
		}else{
			this.$telAxios.post('/v1/librarian/updatePassWord',{
				accountNumber: this.userinfo.account,
				oldPass: this.password.oldpassword,
				passWord: this.password.newpassword
			}).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					this.$confirm("密码修改成功,请重新登陆", "提示", {
						showClose: false,
						distinguishCancelAndClose: false,
						showCancelButton: false,
						closeOnClickModal: false,
						closeOnPressEscape: false,
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					}).then(() => {
						localStorage.clear()
						this.$router.push(
						  { path: "/login" }
						)
					})
				}else{
					this.$message({
						message: res.data.message,
						type: 'warning'
					});
				}
			})
		}
		
    },
    cancel (formName) {
      // this.changePwd = false;
      this.$refs[formName].resetFields();
      this.$router.go(-1);
      // this.title = "账号信息";
    }
  }
};
</script>
<style lang='less' scoped>
</style>